package top.soulblack.purchasesale.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import top.soulblack.purchasesale.model.DTO.GoodDTO;
import top.soulblack.purchasesale.model.DTO.OrderDTO;
import top.soulblack.purchasesale.model.VO.GoodsToQueryVO;
import top.soulblack.purchasesale.model.VO.InOrOutOrderVO;
import top.soulblack.purchasesale.model.VO.UserVo;
import top.soulblack.purchasesale.common.enums.UserRoleEnum;
import top.soulblack.purchasesale.model.User;
import top.soulblack.purchasesale.service.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 韦溜溜
 * @Description:仓库controller
 * @Date: Create in 21:21 2019/5/9
 */
@Controller
@RequestMapping("/stock")
public class StockController {

    @Autowired
    private StoreHouseService storeHouseService;

    @Autowired
    private UserService userService;

    @Autowired
    private RequisitionNoteService requisitionNoteService;

    @Autowired
    private OrderMasterService orderMasterService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private InOutOrderService inOutOrderService;

    @GetMapping("/query")
    public String query (Model model){
        List<GoodsToQueryVO> goodsToQueryVOList = goodsService.selectGoodListToQuery();
        model.addAttribute("goodList",goodsToQueryVOList);
        return "stock/query";
    }

    @GetMapping("/account")
    public String account (){
        return "stock/account";
    }

    @GetMapping("/check_list")
    public String checkList (){
        return "stock/check_list";
    }

    @RequestMapping("/management")
    public String management (@RequestParam(value = "page",defaultValue = "1") Integer page,
                              @RequestParam(value = "size",defaultValue = "5") Integer size,
                              Model model){
        //获取VO分页
        PageInfo pageInfo = storeHouseService.getVoPage(page, size);
       //查找仓库管理员
        List<User> Managers = userService.findByRoleId(UserRoleEnum.STORE_MAN.getCode()) ;
        List<UserVo> userVos = new ArrayList<>();
        Managers.forEach(e->{
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(e,userVo);
            userVos.add(userVo);
        });
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("Managers",userVos);
        return "stock/Management";

    }

    @RequestMapping("/requisition_note")
    public String inOrder (Model model){
        List<GoodDTO> goodsOption = requisitionNoteService.getGoodsOptionForOrder();
        //后期加入权限认证和微信授权就从cookie中获取
        String openid = "fdasdasfsadfsdaurn";
        User user = userService.selectByOpenid(openid);
        List<OrderDTO> orderDTOList = orderMasterService.selectListByAllShopId(user.getShopId());
        orderDTOList = orderDTOList.stream()
                .filter(e->!(e.getOrderStatus().equals(0) && e.getOutShopId().equals(user.getShopId())))
                .collect(Collectors.toList());
        model.addAttribute("goodsOption",goodsOption);
        model.addAttribute("orderDTOList",orderDTOList);
        return "stock/requisition_note";
    }

    @RequestMapping("/out_order")
    public String outOrder (Model model){
        //TODO 后期加入权限认证和微信授权就从cookie中获取
        String openid = "fdasdasfsadfsdaurn";
        User user = userService.selectByOpenid(openid);
        List<OrderDTO> orderDTOList = orderMasterService.selectListByAllShopId(user.getShopId());
        orderDTOList = orderDTOList.stream()
                .filter(e->!(e.getOrderStatus().equals(0) && e.getOutShopId().equals(user.getShopId())))
                .collect(Collectors.toList());
        model.addAttribute("orderDTOList",orderDTOList);
        return "stock/requisition_note";
    }

    @RequestMapping("/in_order")
    public String requisitionNote (Model model){
        //TODO 后期加入权限认证和微信授权就从cookie中获取
        List<InOrOutOrderVO> inOrOutOrderVOS = inOutOrderService.selectInOrder();
        model.addAttribute("inOrOutOrderVOS",inOrOutOrderVOS);
        return "stock/in_order";
    }


}
